const config = require('../util/config')

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
    reqObj.page = reqObj.page || 1
    require('../util/getHtml')({
      url: 'http://api.dataoke.com/index.php?r=Port/index&type=top100&appkey=8cdecbb2ef&v=2&page=' + reqObj.page
    }).then(result => {
      var obj = JSON.parse(result)
      var list = []
      obj.result.forEach(item => {
        list.push({
          id: item.GoodsID,
          img: item.Pic,
          title: item.Title,
          price: item.Org_Price,
          sale: item.Sales_num,
          shopName: '',
          link: (item.IsTmall ? 'https://detail.tmall.com/item.htm?id=' : 'https://item.taobao.com/item.htm?id=') + item.GoodsID,
          rate: Math.round(item.Commission * config.returnRate * 100) / 100
        })
      })
      resolve({
        list: list
      })
    })
  })
}
